﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MM.Common.Base;
using System.Reflection;

namespace MM.Commmon.UI
{
    public partial class FrmBase : XtraForm
    {
        protected bool _CanClose = true;
        private string _FormCode;
        protected string _FunctionParameter;
        private string[] _InformationCode = new string[0];
        protected string _NoCloseMessage = "";

        public FrmBase()
        {
            InitializeComponent();
        }

        private void AddInformationCode()
        {
            if (InformationCode.Length > 0)
            {
                for (int i = 0; i < InformationCode.Length; i++)
                {
                    InsertMessage("+", InformationCode[i]);
                }
                base.FormClosed += new FormClosedEventHandler(DelInformationCode);
            }
        }

        public virtual void CallMessage(bool ispopup, string parm)
        {
        }

        private void DelInformationCode(object sender, FormClosedEventArgs e)
        {
            if (this.InformationCode.Length > 0)
            {
                for (int i = 0; i < this.InformationCode.Length; i++)
                {
                    this.InsertMessage("-", this.InformationCode[i]);
                }
            }
        }

        protected void DisplayError(string object_name, string event_name, Exception ex)
        {
            string message = "";
            if (ex.InnerException != null)
            {
                message = ex.Message + ex.InnerException.Message;
            }
            else
            {
                message = ex.Message;
            }
            Log.WriteError(object_name, event_name, (message.Length > 100) ? message.Substring(0, 100) : message);
            new FrmErrorMessage(ex).ShowDialog();
        }

        protected DialogResult DisplayInfo(string infoDetailCode)
        {
            FrmAlert alert = new FrmAlert(infoDetailCode);
            return alert.ShowDialog();
        }

        public void FadeMessage(string message, double autoFadeSeconds)
        {
            FadeMessage(message, MessageBoxIcon.Asterisk, autoFadeSeconds);
        }

        public void FadeMessage(string message, MessageBoxIcon icon, double autoFadeSeconds)
        {
            new FrmWaiting(message, icon, autoFadeSeconds).Show();
        }

        public virtual bool Init()
        {
            if (!SystemInfo.DbData.IsOpen)
            {
                return false;
            }
            if (((SystemInfo.MessageDataServer.MessageStatus == EMessageStatus.Wait) || (SystemInfo.MessageDataServer.MessageStatus == EMessageStatus.New)) && (this._InformationCode.Length > 0))
            {
                this.AddInformationCode();
            }
            return true;
        }

        public void InsertCall(int id, string body)
        {
            SendMessage item = new SendMessage("###", "I|" + id.ToString() + "|" + body);
            lock (SystemInfo.MessageDataServer.SendMessage)
            {
                SystemInfo.MessageDataServer.SendMessage.Enqueue(item);
            }
        }

        public void InsertCallGroup(string group, string body)
        {
            SendMessage item = new SendMessage("###", "G|" + group + "|" + body);
            lock (SystemInfo.MessageDataServer.SendMessage)
            {
                SystemInfo.MessageDataServer.SendMessage.Enqueue(item);
            }
        }

        public void InsertCallRole(string role, string body)
        {
            SendMessage item = new SendMessage("###", "R|" + role + "|" + body);
            lock (SystemInfo.MessageDataServer.SendMessage)
            {
                SystemInfo.MessageDataServer.SendMessage.Enqueue(item);
            }
        }

        public void InsertFunctionInformation(string code, string body)
        {
            if ((code != "+") && (code != "-"))
            {
                this.InsertMessage(code, body);
            }
        }

        public void InsertMessage(int emp_id, string body)
        {
            SendMessage item = new SendMessage("@" + emp_id.ToString(), body);
            lock (SystemInfo.MessageDataServer.SendMessage)
            {
                SystemInfo.MessageDataServer.SendMessage.Enqueue(item);
            }
        }

        private void InsertMessage(string code, string body)
        {
            SendMessage item = new SendMessage(code, body);
            lock (SystemInfo.MessageDataServer.SendMessage)
            {
                SystemInfo.MessageDataServer.SendMessage.Enqueue(item);
            }
        }

        public DialogResult PopupWindow(string mod, string fun, string para)
        {
            string str = string.Empty;
            return this.PopupWindow(mod, fun, para, ref str);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (!this.CanClose)
            {
                XtraMessageBox.Show(this.NoCloseMessage);
                e.Cancel = true;
            }
            else
            {
                base.OnClosing(e);
            }
        }

        public DialogResult PopupWindow(string mod, string fun, string para, ref string out_para)
        {
            DialogResult abort = DialogResult.Abort;
            string assemblyFile = SystemInfo.RunPath + mod + ".dll";
            try
            {
                FrmBase base2 = (FrmBase)Assembly.LoadFrom(assemblyFile).CreateInstance(mod + "." + fun);
                base2.FunctionParameter = para;
                if (base2.Init())
                {
                    base2.SetOutParameter(ref out_para);
                    return base2.ShowDialog();
                }
                base2.Close();
            }
            catch (Exception exception)
            {
                Log.WriteError("FrmMain", "Run", exception.Message + " " + exception.InnerException);
                XtraMessageBox.Show(exception.Message);
            }
            return abort;
        }

        public virtual void SetOutParameter(ref string out_para)
        {
        }

        public void ZoomWin()
        {
            base.Top = 0;
            base.Left = 0;
            if (base.Parent == null)
            {
                base.Width = Screen.PrimaryScreen.Bounds.Width - 5;
                base.Height = Screen.PrimaryScreen.Bounds.Height - 5;
            }
            else
            {
                base.Width = base.Parent.Width - 5;
                base.Height = base.Parent.Height - 5;
            }
        }

        [Category("MM附加"), Browsable(true), Description("窗体是否允许关闭")]
        public bool CanClose
        {
            get
            {
                return this._CanClose;
            }
            set
            {
                this._CanClose = value;
            }
        }

        [Browsable(true), Category("MM附加"), Description("窗体代码")]
        public string FormCode
        {
            get
            {
                return this._FormCode;
            }
            set
            {
                this._FormCode = value;
            }
        }

        [Browsable(false)]
        public string FunctionParameter
        {
            get
            {
                return this._FunctionParameter;
            }
            set
            {
                this._FunctionParameter = value;
            }
        }

        [Browsable(true), Category("MM附加"), Description("窗体接收信息码")]
        public string[] InformationCode
        {
            get
            {
                return this._InformationCode;
            }
            set
            {
                this._InformationCode = value;
            }
        }

        [Description("窗体不能被关闭的提示信息"), Category("MM附加"), Browsable(true)]
        public string NoCloseMessage
        {
            get
            {
                return this._NoCloseMessage;
            }
            set
            {
                this._NoCloseMessage = value;
            }
        }
    }
}
